package com.grp6.game.controller;

import com.grp6.game.ui.platform.IAbstractUIPlatformFactory;

/**
 * This class initializes the framework. It represents the controller, creates model and view,<br/>  
 * and runs the game lifecycle.  
 * @author Group6
 */
public class GameLord {
	
	/**
	 * This method runs when the game is started which initializes the framework and 
	 * runs the gamelifecycle. The game lifecycle runs until the game comes to an end.
	 * @param args
	 * @throws ClassNotFoundException
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
	public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		if (args.length!=2){
			System.err.println("Synopse: \n"+
					"  java -cp framework.jar;uiPlugin.jar;gamePlugin.jar "
					+GameLord.class.getCanonicalName()+
					" UIPlatformFactoryClass"+
					" GameFactoryClass");
			return;
		}
		
		IAbstractUIPlatformFactory uifac = getGUIFactory(args[0]);
		IAbstractGameFactory gfac = getGameFactory(args[1]);
		
		IGameBuilder gameBuilder = gfac.createGameBuilder(uifac);
		IGameLifecycle lifecycle = gameBuilder.build();		
		
		try{
			lifecycle.runLifecycle();
		}finally{
			uifac.cleanUp();
		}
	}
	
	
	/**
	 * This method is used to load the Concrete implementation of AbstractGameFactory
	 * @param classname 
	 * @return Concrete implementation of AbstractGameFactory
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private static IAbstractGameFactory getGameFactory(String classname) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IAbstractGameFactory) Class.forName(classname).newInstance();
	}
	
	/**
	 * This method is used to load the Concrete implementation of AbstractUIPlatformFactory
	 * @return Concrete class that implements AbstractUIPlatformFactory
	 * @throws ClassNotFoundException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private static IAbstractUIPlatformFactory getGUIFactory(String classname) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		return (IAbstractUIPlatformFactory) Class.forName(classname).newInstance();
	}
}
